/*
 * Created by JFormDesigner on Thu May 26 16:50:02 CST 2022
 */

package jw3.c2.view;

import java.awt.event.*;

import jw3.c2.controller.CustomerController;
import jw3.c2.controller.OpensController;
import jw3.c2.model.Customer;

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class H2 extends JFrame {
    public H2() {
        initComponents();
        setLocation(500,200);
        setSize(1150,800);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
    //1、全查  2、模糊查
    private void showData(String U_Name){
        //方式一：一维数组作为表头、二维数组作为数据  缺陷:需要初始化长度、无法动态增删

        //方式二: 给表格设置模型,使用Vector<Object>完成
        //1、表头
        Vector<Object> heads=new Vector<Object>();
        heads.add("客户编号");
        heads.add("会员编号");
        heads.add("会员姓名");
        heads.add("性别");
        heads.add("身份证号");
        heads.add("联系电话");
        heads.add("详细地址");

        //2、表数据
//        Vector<UserInfo> data= UserInfoController.getAll();
        data= CustomerController.getAll1(U_Name);
        //3、将表头和数据赋值给模型，表格模型构造完成
        DefaultTableModel dtm=new DefaultTableModel(data,heads);
        table1.setModel(dtm);
        //table1=new JTable(dtm);   等同上面，但会增加开销
    }
    private void showData1(String Vip_ID){
        //方式一：一维数组作为表头、二维数组作为数据  缺陷:需要初始化长度、无法动态增删

        //方式二: 给表格设置模型,使用Vector<Object>完成
        //1、表头
        Vector<Object> heads=new Vector<Object>();
        heads.add("客户编号");
        heads.add("会员编号");
        heads.add("会员姓名");
        heads.add("性别");
        heads.add("身份证号");
        heads.add("联系电话");
        heads.add("详细地址");

        //2、表数据
//        Vector<UserInfo> data= UserInfoController.getAll();
        data= CustomerController.getAll2(Vip_ID);
        //3、将表头和数据赋值给模型，表格模型构造完成
        DefaultTableModel dtm=new DefaultTableModel(data,heads);
        table1.setModel(dtm);
        //table1=new JTable(dtm);   等同上面，但会增加开销
    }
    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        showData(textField1.getText());
    }

    private void menu1MouseClicked(MouseEvent e) {
        // TODO add your code here
        H1 h1=new H1();
        setVisible(false);
    }

    private void button5MouseClicked(MouseEvent e) {
        // TODO add your code here
        int index=table1.getSelectedRow();//获取选择第一行的所索引
        if (index!=-1){//是否选中了一行
            if (JOptionPane.showConfirmDialog(null,
                    "您是否确认删除此数据?")==0){//是否点击了确定
                //取id   data、index  data是二维集合，第一个get获取了行、第二个get获取了列
                int id=(int)data.get(index).get(0);
                //调用控制器完成删除
                if (CustomerController.logOut(id)){
                    showData("");//自动刷新
                    JOptionPane.showMessageDialog(null,
                            "删除成功!");

                }else{
                    JOptionPane.showMessageDialog(null,
                            "删除失败!");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null,
                    "您未选中任意一行!");
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
            showData(textField1.getText());
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
        dialog1.setModal(true); //模态框
        dialog1.setVisible(true);//显示出来
    }

    private void button6MouseClicked(MouseEvent e) {
        // TODO add your code here
        Customer ui=new Customer();
        //不用管id 对象中int不写默认为0  且id为自增

        if (!textField2.getText().equals("")){
            ui.setVip_ID(Integer.parseInt((textField2.getText())));
//        }else{
//            JOptionPane.showMessageDialog(null,
//                    "会员编号不能为空!");
        }
        ui.setU_Name(textField3.getText());
        ui.setU_Sex(comboBox1.getSelectedItem().toString());
        ui.setU_D(textField5.getText());
        ui.setU_Phone(textField6.getText());
        ui.setU_A(textField7.getText());

        //2、简单判断

                if (!ui.getU_Name().equals("") && !ui.getU_A().equals("") && !ui.getU_Phone().equals("") && !ui.getU_A().equals("") && !ui
                        .getU_D().equals("")) {
                    if (ui.getVip_ID()>=1 && ui.getVip_ID()<=3){
                    //唯一键重复
                    for (Vector<Object> item : data) {
                        if (item.contains(ui.getU_D())) {
                            JOptionPane.showMessageDialog(null,
                                    "该身份证号已存在!");
                            return;
                        }
                    }
                    if (CustomerController.register(ui)) {
                        showData("");//自动刷新
                        JOptionPane.showMessageDialog(null,
                                "添加成功!");
                        dialog1.dispose();//关闭对话框
                    } else {
                        JOptionPane.showMessageDialog(null,
                                "添加失败!");
                    }
            }else {
                JOptionPane.showMessageDialog(null,"会员编号只能为1,2,3!");
            }
    }else {
            JOptionPane.showMessageDialog(null,"添加信息不能有空!");
        }
    }

    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here
        int index=table1.getSelectedRow();//选中行的索引
        if (index!=-1){//是否选中了一行
            //获取值 data、index
            //先传值
            label6.setText(data.get(index).get(0).toString());
            textField4.setText(data.get(index).get(1).toString());
            textField8.setText(data.get(index).get(2).toString());
            comboBox2.setSelectedItem(data.get(index).get(3).toString());
            textField9.setText(data.get(index).get(4).toString());
            textField10.setText(data.get(index).get(5).toString());
            textField11.setText(data.get(index).get(6).toString());
            //后展示对话框
            dialog2.setModal(true);
            dialog2.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(null,
                    "您未选中任意一行!");
        }

    }
    //2、简单判断
    private void button7MouseClicked(MouseEvent e) {
        // TODO add your code here
        Customer ui =new Customer();
        if (!textField2.getText().equals("")){
            ui.setVip_ID(Integer.parseInt((textField2.getText())));
//        }else{
//            JOptionPane.showMessageDialog(null,
//                    "会员编号不能为空!");
        }
        if (!textField4.getText().equals("")){
            ui.setU_ID(Integer.parseInt(label6.getText()));
            ui.setVip_ID(Integer.parseInt((textField4.getText())));}
        ui.setU_Name(textField8.getText());
        ui.setU_Sex(comboBox2.getSelectedItem().toString());
        ui.setU_D(textField9.getText());
        ui.setU_Phone(textField10.getText());
        ui.setU_A(textField11.getText());
        if (!ui.getU_Name().equals("") && !ui.getU_A().equals("") && !ui.getU_Phone().equals("") && !ui.getU_A().equals("")) {
            if (ui.getVip_ID()>=1 && ui.getVip_ID()<=3){
                //唯一键重复
                for (Vector<Object> item : data) {
                    if (item.contains(ui.getU_D())) {
                        JOptionPane.showMessageDialog(null,
                                "该身份证号已存在!");
                        return;
                    }
                }
                if (CustomerController.edit(ui)) {
                    showData("");//自动刷新
                    JOptionPane.showMessageDialog(null,
                            "更新成功!");
                    dialog2.dispose();//关闭对话框
                } else {
                    JOptionPane.showMessageDialog(null,
                            "更新失败!");
                }
            }else {
                JOptionPane.showMessageDialog(null,"会员编号只能为1,2,3!");
            }
        }else {
            JOptionPane.showMessageDialog(null,"修改信息不能有空!");
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        menu2 = new JMenu();
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        label17 = new JLabel();
        dialog1 = new JDialog();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        textField3 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        textField7 = new JTextField();
        button6 = new JButton();
        comboBox1 = new JComboBox();
        dialog2 = new JDialog();
        label9 = new JLabel();
        label10 = new JLabel();
        textField4 = new JTextField();
        label11 = new JLabel();
        label12 = new JLabel();
        label13 = new JLabel();
        label14 = new JLabel();
        label15 = new JLabel();
        textField8 = new JTextField();
        textField9 = new JTextField();
        textField10 = new JTextField();
        textField11 = new JTextField();
        button7 = new JButton();
        comboBox2 = new JComboBox();
        label16 = new JLabel();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {

            //======== menu1 ========
            {
                menu1.setText("\u6765\u5bbe\u4fe1\u606f\u4e00\u89c8\u8868");
                menu1.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        menu1MouseClicked(e);
                    }
                });
            }
            menuBar1.add(menu1);

            //======== menu2 ========
            {
                menu2.setText("\u4f1a\u5458\u57fa\u672c\u4fe1\u606f\u7ef4\u62a4");
            }
            menuBar1.add(menu2);
        }
        contentPane.add(menuBar1);
        menuBar1.setBounds(0, 0, menuBar1.getPreferredSize().width, 22);

        //---- label1 ----
        label1.setText("\u4f1a\u5458\u7f16\u53f7/\u59d3\u540d\uff1a");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(45, 72), label1.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(195, 65, 130, 30);

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(360, 65), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u5237\u65b0");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(480, 65), button2.getPreferredSize()));

        //======== panel1 ========
        {
            panel1.setLayout(new BorderLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1, BorderLayout.CENTER);
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 140, 1085, 170);

        //---- button3 ----
        button3.setText("\u6dfb\u52a0");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(245, 405), button3.getPreferredSize()));

        //---- button4 ----
        button4.setText("\u4fee\u6539");
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button4MouseClicked(e);
            }
        });
        contentPane.add(button4);
        button4.setBounds(new Rectangle(new Point(385, 405), button4.getPreferredSize()));

        //---- button5 ----
        button5.setText("\u5220\u9664");
        button5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button5MouseClicked(e);
            }
        });
        contentPane.add(button5);
        button5.setBounds(new Rectangle(new Point(530, 405), button5.getPreferredSize()));

        //---- label17 ----
        label17.setText("text");
        label17.setIcon(new ImageIcon("C:\\Users\\\u5c0f\u6797\\Desktop\\B.jpg"));
        contentPane.add(label17);
        label17.setBounds(new Rectangle(new Point(-50, -35), label17.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);

            //---- label2 ----
            label2.setText("\u5ba2\u6237\u7f16\u53f7\uff1a");
            dialog1ContentPane.add(label2);
            label2.setBounds(new Rectangle(new Point(30, 15), label2.getPreferredSize()));

            //---- label3 ----
            label3.setText("\u4f1a\u5458\u7f16\u53f7\uff1a");
            dialog1ContentPane.add(label3);
            label3.setBounds(new Rectangle(new Point(30, 50), label3.getPreferredSize()));
            dialog1ContentPane.add(textField2);
            textField2.setBounds(100, 43, 105, textField2.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u5ba2\u6237\u59d3\u540d\uff1a");
            dialog1ContentPane.add(label4);
            label4.setBounds(new Rectangle(new Point(30, 90), label4.getPreferredSize()));

            //---- label5 ----
            label5.setText("\u5ba2\u6237\u6027\u522b\uff1a");
            dialog1ContentPane.add(label5);
            label5.setBounds(new Rectangle(new Point(30, 130), label5.getPreferredSize()));

            //---- label6 ----
            label6.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            dialog1ContentPane.add(label6);
            label6.setBounds(new Rectangle(new Point(30, 210), label6.getPreferredSize()));

            //---- label7 ----
            label7.setText("\u8eab\u4efd\u8bc1\u53f7\uff1a");
            dialog1ContentPane.add(label7);
            label7.setBounds(new Rectangle(new Point(30, 170), label7.getPreferredSize()));

            //---- label8 ----
            label8.setText("\u8be6\u7ec6\u5730\u5740\uff1a");
            dialog1ContentPane.add(label8);
            label8.setBounds(new Rectangle(new Point(30, 250), label8.getPreferredSize()));
            dialog1ContentPane.add(textField3);
            textField3.setBounds(100, 83, 105, textField3.getPreferredSize().height);
            dialog1ContentPane.add(textField5);
            textField5.setBounds(100, 165, 105, textField5.getPreferredSize().height);
            dialog1ContentPane.add(textField6);
            textField6.setBounds(100, 200, 105, textField6.getPreferredSize().height);
            dialog1ContentPane.add(textField7);
            textField7.setBounds(100, 243, 105, textField7.getPreferredSize().height);

            //---- button6 ----
            button6.setText("\u786e\u5b9a");
            button6.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button6MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button6);
            button6.setBounds(new Rectangle(new Point(105, 300), button6.getPreferredSize()));
            dialog1ContentPane.add(comboBox1);
            comboBox1.setBounds(100, 123, 90, comboBox1.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }

        //======== dialog2 ========
        {
            Container dialog2ContentPane = dialog2.getContentPane();
            dialog2ContentPane.setLayout(null);

            //---- label9 ----
            label9.setText("\u5ba2\u6237\u7f16\u53f7\uff1a");
            dialog2ContentPane.add(label9);
            label9.setBounds(new Rectangle(new Point(30, 15), label9.getPreferredSize()));

            //---- label10 ----
            label10.setText("\u4f1a\u5458\u7f16\u53f7\uff1a");
            dialog2ContentPane.add(label10);
            label10.setBounds(new Rectangle(new Point(30, 50), label10.getPreferredSize()));
            dialog2ContentPane.add(textField4);
            textField4.setBounds(100, 43, 105, textField4.getPreferredSize().height);

            //---- label11 ----
            label11.setText("\u5ba2\u6237\u59d3\u540d\uff1a");
            dialog2ContentPane.add(label11);
            label11.setBounds(new Rectangle(new Point(30, 90), label11.getPreferredSize()));

            //---- label12 ----
            label12.setText("\u5ba2\u6237\u6027\u522b\uff1a");
            dialog2ContentPane.add(label12);
            label12.setBounds(new Rectangle(new Point(30, 130), label12.getPreferredSize()));

            //---- label13 ----
            label13.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            dialog2ContentPane.add(label13);
            label13.setBounds(new Rectangle(new Point(30, 210), label13.getPreferredSize()));

            //---- label14 ----
            label14.setText("\u8eab\u4efd\u8bc1\u53f7\uff1a");
            dialog2ContentPane.add(label14);
            label14.setBounds(new Rectangle(new Point(30, 170), label14.getPreferredSize()));

            //---- label15 ----
            label15.setText("\u8be6\u7ec6\u5730\u5740\uff1a");
            dialog2ContentPane.add(label15);
            label15.setBounds(new Rectangle(new Point(30, 250), label15.getPreferredSize()));
            dialog2ContentPane.add(textField8);
            textField8.setBounds(100, 83, 105, textField8.getPreferredSize().height);
            dialog2ContentPane.add(textField9);
            textField9.setBounds(100, 165, 105, textField9.getPreferredSize().height);
            dialog2ContentPane.add(textField10);
            textField10.setBounds(100, 200, 105, textField10.getPreferredSize().height);
            dialog2ContentPane.add(textField11);
            textField11.setBounds(100, 243, 105, textField11.getPreferredSize().height);

            //---- button7 ----
            button7.setText("\u786e\u5b9a");
            button7.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button7MouseClicked(e);
                }
            });
            dialog2ContentPane.add(button7);
            button7.setBounds(new Rectangle(new Point(105, 300), button7.getPreferredSize()));
            dialog2ContentPane.add(comboBox2);
            comboBox2.setBounds(100, 123, 90, comboBox2.getPreferredSize().height);

            //---- label16 ----
            label16.setText("text");
            dialog2ContentPane.add(label16);
            label16.setBounds(new Rectangle(new Point(105, 15), label16.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog2ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog2ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog2ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog2ContentPane.setMinimumSize(preferredSize);
                dialog2ContentPane.setPreferredSize(preferredSize);
            }
            dialog2.pack();
            dialog2.setLocationRelativeTo(dialog2.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        comboBox1.addItem("男");
        comboBox1.addItem("女");
        comboBox2.addItem("男");
        comboBox2.addItem("女");
        showData("");
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JMenu menu2;
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JLabel label17;
    private JDialog dialog1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JTextField textField3;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JButton button6;
    private JComboBox comboBox1;
    private JDialog dialog2;
    private JLabel label9;
    private JLabel label10;
    private JTextField textField4;
    private JLabel label11;
    private JLabel label12;
    private JLabel label13;
    private JLabel label14;
    private JLabel label15;
    private JTextField textField8;
    private JTextField textField9;
    private JTextField textField10;
    private JTextField textField11;
    private JButton button7;
    private JComboBox comboBox2;
    private JLabel label16;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    private static Vector<Vector<Object>> data;
}
